<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { PageQuery } from '#/api/common';
import type { DictData } from '#/api/system/dict/dict-data';

import { ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { Modal, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import { dictDataList, dictDataRemove } from '#/api/system/dict/dict-data';
import dictDataDrawer from '#/views/system/dict/data/dict-data-drawer.vue';

import { emitter } from '../mitt';
import { columns, querySchema } from './data';

const dictType = ref('');

const formOptions: VbenFormProps = {
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2',
};

const gridOptions: VxeGridProps = {
  columns: columns(),
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const params: PageQuery = {
          current: page.currentPage,
          size: page.pageSize,
          ...formValues,
        };
        if (dictType.value) {
          params.dictType = dictType.value;
        }
        return await dictDataList(params);
      },
    },
  },
  id: 'system-dict-data-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [DictDataDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: dictDataDrawer,
});

function handleAdd() {
  drawerApi.setData({ dictType: dictType.value });
  drawerApi.open();
}

async function handleEdit(record: DictData) {
  drawerApi.setData({
    id: record.id,
  });
  drawerApi.open();
}

async function handleDelete(row: DictData) {
  await dictDataRemove([row.id]);
  await tableApi.query();
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: DictData) => row.id);
  Modal.confirm({
    title: $t('common.tip'),
    okType: 'danger',
    content: $t('ui.actionMessage.deleteConfirm', [ids.length]),
    onOk: async () => {
      await dictDataRemove(ids);
      await tableApi.query();
    },
  });
}

emitter.on('rowClick', async (value) => {
  dictType.value = value;
  await tableApi.query();
});
</script>

<template>
  <div>
    <BasicTable id="dict-data" :table-title="$t('system.dictData.tableTitle')">
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            @click="handleMultiDelete"
          >
            {{ $t('common.delete') }}
          </a-button>
          <a-button
            :disabled="dictType === ''"
            type="primary"
            @click="handleAdd"
          >
            {{ $t('common.create') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <GhostButton @click="handleEdit(row)">
            {{ $t('common.edit') }}
          </GhostButton>
          <Popconfirm
            :get-popup-container="
              (node) => getVxePopupContainer(node, 'dict-data')
            "
            placement="left"
            :title="$t('common.deleteConfirm')"
            @confirm="handleDelete(row)"
          >
            <GhostButton danger @click.stop="">
              {{ $t('common.delete') }}
            </GhostButton>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <DictDataDrawer @reload="tableApi.query()" />
  </div>
</template>
